<?php

class __admin_system_guides extends Module {
	
	public function __construct () {
		parent::__construct('system');
	}
	
	public function guides () {
		$response = $this->getGuides ();
		$response->setTemplate ('system/guides/list');
		
		if (getRequest ('created','bool')) {
			$response->addObject (new SystemMessage (lang('admin_guide_created','system')));
		}
		if (getRequest ('saved','bool')) {
			$response->addObject (new SystemMessage (lang('admin_guide_saved','system')));
		}
		if (getRequest ('deleted','bool')) {
			$response->addObject (new SystemMessage (lang('admin_guide_deleted','system')));
		}
		
		return $response;
	}
	
	public function add_guide () {
		return $this->formGuide ();
	}
	
	public function edit_guide () {
		return $this->formGuide (getRequest ('param0','int'));
	}
	
	public function delete_guide () {
		$this->expectSecureGET ();
		
		$oTypeGuide = TypesController::getInstance()->getTypeByName ('explay_guide');
		
		if (!$oGuide = ObjectsController::getInstance()->getObject ($oTypeGuide, getRequest ('param0','int'))) {
			throw new NotFound;
		}
		
		$oGuide->delete ();
		redirect (HTTP_HOST . '/admin/system/guides?deleted');
	}
	
	public function getGuides () {
		$typesController = TypesController::getInstance ();
		$oTypeGuide = $typesController->getTypeByName ('explay_guide');
		
		$sel = new Selection;
		$sel->addType ($oTypeGuide);
		
		$aObjects = ObjectsController::getInstance()->getObjectsList ($sel->getList ());
		
		$response = new ModuleResponse;
		
		foreach ($aObjects as $object) {
			$object->setAttribute ('guide-type-title', $typesController->getTypeById($object->getValue ('type_id'))->getTitle ());
			$response->addObject ($object);
		}
		
		return $response;
	}
	
	private function formGuide ($id = 'new') {
		$oType = TypesController::getInstance()->getTypeByName ('explay_guide');
		
		$form = new Form ($oType, $id);
		$form->buildFromType ();
		
		$aValues = array ();
		$aErrors = array ();
		
		if ($id != 'new') {
			if (!$object = ObjectsController::getInstance()->getObject ($oType, $id)) {
				throw new NotFound;
			}
			
			$aValues = $object->getValues ();
		}
		
		if ((getRequest ('param1') == 'save' && $id != 'new') || (getRequest ('param0') == 'save' && $id == 'new')) {
			$aValues = $this->getRequestData ($id, $oType);
			
			if ($id == 'new') {
				$object = new Object ($oType);
				$object->setValues ($aValues);
				
				$aErrors = $this->getGuideValidate ($object);
				
				if (!count ($aErrors)) {
					$object->createNew ();
					
					redirect (HTTP_HOST . '/admin/system/guides?created');
				}
			} else {
				$object->setValues ($aValues);
				
				$aErrors = $this->getGuideValidate ($object, $id);
				
				if (!count ($aErrors)) {
					$tableName = $object->getValue ('table_name');
					
					$object->save ();
					
					redirect (HTTP_HOST . '/admin/system/guides?saved');
				}
			}
		}
		
		$form->setErrors ($aErrors);
		$form->setValues ($aValues);
		
		$response = new ModuleResponse;
		$response->setTemplate ('system/guides/form');
		$response->addObject ($form);
		
		return $response;
	}
	
	private function getGuideValidate (Object $object, $id = false) {
		$aErros = array ();
		
		if (!TypesController::getInstance()->getTypeById ($object->getValue ('type_id'))) {
			$aErrors['type_id'] = lang('error_admin_guide_undefined_type','system');
			return $aErrors;
		}
		
		$sel = new Selection;
		$sel->addType ($object->getType ());
		$sel->addFilter ('type_id', $object->getValue ('type_id'));
		if ($id !== false) {
			$sel->addFilter ('id', $id, '!=');
		}
		
		if ($sel->getTotalCount () != 0) {
			$aErrors['type_id'] = lang('error_admin_guide_type_exists','system');
			return $aErrors;
		}
		
		if (!$oField = FieldsController::getInstance()->getField ($object->getValue ('label_field_id'))) {
			$aErrors['label_field_id'] = lang('error_admin_guide_incorrect_field','system');
			return $aErrors;
		}
		
		if ($oField->getTypeId () != $object->getValue ('type_id')) {
			$aErrors['label_field_id'] = lang('error_admin_guide_incorrect_field','system');
		}
		
		return $aErrors;
	}
	
}